package com.esri.core.map.popup;

import com.esri.core.internal.util.d;
import com.esri.core.io.EsriServiceException;
import java.io.StringWriter;
import java.util.ArrayList;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public abstract class PopupMediaValue {

    /* loaded from: classes.dex */
    public enum VALUE_TYPE {
        IMAGE,
        CHART
    }

    public abstract VALUE_TYPE getType();

    public static PopupMediaValue fromJson(JsonParser jsonParser) throws Exception {
        if (!d.c(jsonParser)) {
            throw new EsriServiceException("This response cannot be parsed.");
        }
        PopupMediaValue popupMediaValue = null;
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("sourceURL".equals(currentName)) {
                if (popupMediaValue == null) {
                    popupMediaValue = new PopupImageMediaValue();
                }
                ((PopupImageMediaValue) popupMediaValue).a = jsonParser.getText();
            } else if ("linkURL".equals(currentName)) {
                if (popupMediaValue == null) {
                    popupMediaValue = new PopupImageMediaValue();
                }
                ((PopupImageMediaValue) popupMediaValue).b = jsonParser.getText();
            } else if ("fields".equals(currentName)) {
                if (popupMediaValue == null) {
                    popupMediaValue = new PopupChartMediaValue();
                }
                ArrayList arrayList = new ArrayList();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    arrayList.add(jsonParser.getText());
                }
                ((PopupChartMediaValue) popupMediaValue).a = (String[]) arrayList.toArray(new String[0]);
            } else if ("normalizeField".equals(currentName)) {
                if (popupMediaValue == null) {
                    popupMediaValue = new PopupChartMediaValue();
                }
                ((PopupChartMediaValue) popupMediaValue).b = jsonParser.getText();
            } else {
                jsonParser.skipChildren();
            }
        }
        return popupMediaValue;
    }

    public String toJson() throws Exception {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        if (this instanceof PopupImageMediaValue) {
            PopupImageMediaValue popupImageMediaValue = (PopupImageMediaValue) this;
            if (popupImageMediaValue.a != null) {
                a.writeStringField("sourceURL", popupImageMediaValue.a);
            }
            if (popupImageMediaValue.b != null) {
                a.writeStringField("linkURL", popupImageMediaValue.b);
            }
        } else if (this instanceof PopupChartMediaValue) {
            PopupChartMediaValue popupChartMediaValue = (PopupChartMediaValue) this;
            if (popupChartMediaValue.a != null) {
                a.writeArrayFieldStart("fields");
                for (String str : popupChartMediaValue.a) {
                    a.writeString(str);
                }
                a.writeEndArray();
            }
            if (popupChartMediaValue.b != null) {
                a.writeStringField("normalizeField", popupChartMediaValue.b);
            }
        }
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }
}
